<?php require_once 'httpFilter.php'; 'conf.php'; ?>
<?php require_once 'classes.php'; ?>
<?php
require_once "services/budgetServices.php";

$budgetServices = new BudgetService;
$service = new commonservices();

$budgetId =  $_REQUEST["budgetId"];
if ($budgetId == "current"){
	$budgetId = $service->getCurrentBudgetId($_SESSION['userid']);
}

$carryOver =  $_REQUEST["carryOver"];

$incomeRowIds =  split(",", $_REQUEST["incomeRowId"]);
$incomeBudgets =  split(",",$_REQUEST["incomeBudget"]);
$incomeActuals =  split(",",$_REQUEST["incomeActual"]);
$incomeDescriptions =  split(",",$_REQUEST["incomeDescription"]);

$variableRowIds =  split(",", $_REQUEST["variableRowId"]);
$variableBudgets =  split(",",$_REQUEST["variableBudget"]);
$variableActuals =  split(",",$_REQUEST["variableActual"]);
$variableDescriptions =  split(",",$_REQUEST["variableDescription"]);

$fixedRowIds =  split(",", $_REQUEST["fixedRowId"]);
$fixedBudgets =  split(",",$_REQUEST["fixedBudget"]);
$fixedActuals =  split(",",$_REQUEST["fixedActual"]);
$fixedDescriptions =  split(",",$_REQUEST["fixedDescription"]);


$expenseSavingsRowIds =  split(",", $_REQUEST["expenseSavingsRowId"]);
$expenseSavingsBudgets =  split(",",$_REQUEST["expenseSavingsBudget"]);
$expenseSavingsActuals =  split(",",$_REQUEST["expenseSavingsActual"]);
$expenseSavingsDescriptions =  split(",",$_REQUEST["expenseSavingsDescription"]);

$savingsRowIds =  split(",", $_REQUEST["savingsRowId"]);
$savingsBudgets =  split(",",$_REQUEST["savingsBudget"]);
$savingsActuals =  split(",",$_REQUEST["savingsActual"]);
$savingsDescriptions =  split(",",$_REQUEST["savingsDescription"]);

$errCode=0;
$con = mysql_connect(GlobalConfig::db_host_name,GlobalConfig::db_username,GlobalConfig::db_password);
if (!$con)
{
	die('Could not connect: ' . mysql_error());
}

mysql_select_db(GlobalConfig::db_name, $con);

/*** CARRY OVER ***/
$query = "update budget set carryover=". $carryOver ." where id=". $budgetId;
error_log($query);
$result = mysql_query($query);
if(!$result){
	$errCode=1;
}



/*** INCOME ***/
$count=0;
foreach ($incomeRowIds as  $row){
	if($count != 0){
		if ($row == 0){
			//Insert the new budgetitem. This is for completely new budgetitems
			$query = "insert into  budgetitem (categoryId, expense, description, userid) values (1, 0, '". $incomeDescriptions[$count]  ."', ". $_SESSION["userid"] .")";
			error_log($query);
			$result = mysql_query($query);
			if(!$result){
				$errCode=1;
			}

			$query = "insert into budget_budgetitem (budgetid, budgetitemid, budgetAmount, actualAmount) values (". $budgetId .", (select max(id) as id from budgetitem), ". $incomeBudgets[$count] .", 0)";
		} else {
			//We first check if the budgetitem exists in the budget
			$query = "insert into  budget_budgetitem (budgetid, budgetitemid, budgetAmount, actualAmount) values (". $budgetId .", " . $row . ", " . $incomeBudgets[$count] . ", " . $incomeActuals[$count] . ")";
			error_log($query);
			$result = mysql_query($query);
			if($result){
				continue;
			}

			$query = "update budget_budgetitem set budgetAmount=" . $incomeBudgets[$count] . ", actualAmount=" . $incomeActuals[$count] . " where budgetitemid=" . $row . " and budgetId=" . $budgetId;

			error_log($query);
			$result = mysql_query($query);
			if(!$result){
				$errCode=1;
			}
		}



	}

	$count = $count + 1;
}

/*** Monthly Variable Expenses ***/
$count=0;
foreach ($variableRowIds as  $row){
	if($count != 0){
		if ($row == 0){
			$query = "insert into  budgetitem (categoryId, expense, description, userid) values (2, 0, '". $variableDescriptions[$count]  ."', ". $_SESSION["userid"] .")";
			error_log($query);
			$result = mysql_query($query);
			if(!$result){
				$errCode=1;
			}


			$query = "insert into budget_budgetitem (budgetid, budgetitemid, budgetAmount, actualAmount) values (". $budgetId .", (select max(id) as id from budgetitem), ". $variableBudgets[$count] .", 0)";
		} else {
			//We first check if the budgetitem exists in the budget
			$query = "insert into  budget_budgetitem (budgetid, budgetitemid, budgetAmount, actualAmount) values (". $budgetId .", " . $row . ", " . $variableBudgets[$count] . ", " . $variableActuals[$count] . ")";
			error_log($query);
			$result = mysql_query($query);
			if($result){
				continue;
			}

			$query = "update budget_budgetitem set budgetAmount=" . $variableBudgets[$count] . ", actualAmount=" . $variableActuals[$count] . " where budgetitemid=" . $row . " and budgetId=" . $budgetId;
		}

		error_log($query);
		$result = mysql_query($query);
		if(!$result){
			$errCode=1;
		}
	}

	$count = $count + 1;
}

/*** Monthly Fixed Expenses ***/
$count=0;
foreach ($fixedRowIds as  $row){
	if($count != 0){
		if ($row == 0){
			$query = "insert into  budgetitem (categoryId, expense, description, userid) values (3, 0, '". $fixedDescriptions[$count]  ."', ". $_SESSION["userid"] .")";
			error_log($query);
			$result = mysql_query($query);
			if(!$result){
				$errCode=1;
			}

			$query = "insert into budget_budgetitem (budgetid, budgetitemid, budgetAmount, actualAmount) values (". $budgetId .", (select max(id) as id from budgetitem), ". $fixedBudgets[$count] .", 0)";
		} else {
			//We first check if the budgetitem exists in the budget
			$query = "insert into  budget_budgetitem (budgetid, budgetitemid, budgetAmount, actualAmount) values (". $budgetId .", " . $row . ", " . $fixedBudgets[$count] . ", " . $fixedActuals[$count] . ")";
			error_log($query);
			$result = mysql_query($query);
			if($result){
				continue;
			}

			$query = "update budget_budgetitem set budgetAmount=" . $fixedBudgets[$count] . ", actualAmount=" . $fixedActuals[$count] . " where budgetitemid=" . $row . " and budgetId=" . $budgetId;
		}
		error_log($query);
		$result = mysql_query($query);
		if(!$result){
			$errCode=1;
		}
	}
	$count = $count + 1;
}

/*** Monthly Expenses Savings ***/
$count=0;
foreach ($expenseSavingsRowIds as  $row){
	if($count != 0){
		if ($row == 0){
			$budgetItemId = $budgetServices->createBudgetItem($_SESSION["userid"], 4, $expenseSavingsDescriptions[$count]);
			error_log("Budget ITEM ID: ".$budgetItemId);
			$savingsAccountId = $budgetServices->getSavingsAccountId($_SESSION["userid"]);

			$subAccId = $budgetServices->createSubAccount($savingsAccountId, $budgetItemId, 0);
			error_log("SUB ACCOUNTID : ".$subAccId." AND SAVINGSID: ".$savingsAccountId);

			$query = "insert into budget_budgetitem (budgetid, budgetitemid, budgetAmount, actualAmount) values (". $budgetId .", ". $budgetItemId .", ". $expenseSavingsBudgets[$count] .", 0)";

		} else {
			//We first check if the budgetitem exists in the budget
			$budgetServices-> insertBudgetItemIntoBudget($row, $budgetId, $expenseSavingsBudgets[$count], $expenseSavingsActuals[$count]);

			$query = "update budget_budgetitem set budgetAmount=" . $expenseSavingsBudgets[$count] . ", actualAmount=" . $expenseSavingsActuals[$count] . " where budgetitemid=" . $row . " and budgetId=" . $budgetId;
		}
		error_log($query);
		$result = mysql_query($query);
		if(!$result){
			$errCode=1;
		}

	}
	$count = $count + 1;
}

/*** Monthly Savings ***/
$count=0;
foreach ($savingsRowIds as  $row){
	if($count != 0){
		if ($row == 0){
			$query = "insert into  budgetitem (categoryId, expense, description, userid) values (5, 0, '". $savingsDescriptions[$count]  ."', ". $_SESSION["userid"] .")";
			error_log($query);
			$result = mysql_query($query);
			if(!$result){
				$errCode=1;
			}

			$query = "insert into budget_budgetitem (budgetid, budgetitemid, budgetAmount, actualAmount) values (". $budgetId .", (select max(id) as id from budgetitem), ". $savingsBudgets[$count] .", 0)";
		} else {
			//We first check if the budgetitem exists in the budget
			$query = "insert into  budget_budgetitem (budgetid, budgetitemid, budgetAmount, actualAmount) values (". $budgetId .", " . $row . ", " . $savingsBudgets[$count] . ", " . $savingsActuals[$count] . ")";
			error_log($query);
			$result = mysql_query($query);
			if($result){
				continue;
			}

			$query = "update budget_budgetitem set budgetAmount=" . $savingsBudgets[$count] . ", actualAmount=" . $savingsActuals[$count] . " where budgetitemid=" . $row . " and budgetId=" . $budgetId;
		}
		error_log($query);
		$result = mysql_query($query);
		if(!$result){
			$errCode=1;
		}
	}
	$count = $count + 1;
}

mysql_close($con);
?>
<result>
<code> <?=$errCode?> </code>
</result>
